import maya.cmds as cmds
import pymel.core as pm;

cmds.SelectAll();
cmds.Delete();

pulleyRadius = 25;
ropeDiameter = 3*1.15;
lipHeight = 1;
lipWall =0.1;
screwDiameter = 7.9375*1.10;#5/16 in bolt to mm
boltMountDistance = pulleyRadius / 2;#purposefully using integer division here to get even nubmer
wallthickness = 5;
washerThickness = 1.5875;#5/16 in bolt washer = 1/16 in thickness to mm

pulleyheight = ropeDiameter + 2*lipHeight;
pulleyClearance = 10;#distance between pulley and bottom of cnclosure

enclosureDepth = 2*pulleyRadius + 2*ropeDiameter + 2;
enclosureHeight = pulleyRadius + 1.25*screwDiameter + pulleyClearance;
enclosureWidth = 2*pulleyheight+2*wallthickness + 4*washerThickness + 0.1;#0.1 fudge factor




def createPulley( x, z):
    pulley = cmds.polyPipe(r=pulleyRadius,h=pulleyheight*2,t=(pulleyRadius-(screwDiameter/2.0)))#polypipe bug, where height is created at half height. :(
    sqGuide = cmds.polyPipe(r=pulleyRadius,h=(pulleyheight*2 - lipHeight*2),t=(lipWall))#polypipe bug, where height is created at half height. :(
    cmds.select(pulley[0],sqGuide[0]);
    pulley = cmds.polyBoolOp(op=2);
    guide = cmds.polyTorus( sx=20, sy=20, r=(pulleyRadius-lipWall), sr=(ropeDiameter/2.0 +lipHeight/2.0) )
    cmds.select(pulley[0],guide[0]);
    pulley = cmds.polyBoolOp(op=2);
    #cmds.rotate(0,0,'90deg');
    cmds.move(x,0,z);
    return pulley;
 
def createRope(x , z):
    rope = cmds.polyCylinder( sx=20, sy=15, sz=5, h=4*pulleyRadius,r=ropeDiameter);#purposely making r = diameter to double hole size
    cmds.move(x,0,z);
    return rope;
    
def createBoltHole(z):
    bolt = cmds.polyCylinder( sx=20, sy=15, sz=5, h=4*pulleyRadius,r=screwDiameter/2.0);#purposely making r = diameter to double hole size
    cmds.move(0,0,z);
    return bolt;

def subtractObject(object1,object2):
    cmds.select(object1[0],object2[0]);
    returnObject = cmds.polyBoolOp(op=2);
    return returnObject;
    
    
    
 
bolt = cmds.polyCylinder( sx=10, sy=15, sz=5, h=4*pulleyRadius,r=screwDiameter/2.0);
cmds.rotate(0,0,'90deg');




enclosure = cmds.polyCube(w=enclosureWidth, h=enclosureHeight,sy=5,d=enclosureDepth);
inside = cmds.polyCube(w=enclosureWidth - 2*wallthickness,sy=5, h=enclosureHeight,d=enclosureDepth);
cmds.select(inside[0],r=True);
cmds.move(0,wallthickness, 0);
cmds.select(enclosure[0],inside[0],r=True);
enclosure = cmds.polyBoolOp(op=2);
#cmds.nonLinear( type='flare', endFlareZ = 0.15 );
  
#now place enclosure with bottom at 0
cmds.move(0,enclosureHeight/2.0,0)
  
#now move enclosure down to appropriate height, so that height of enclosure doesn't affect bottom position of enclosure
cmds.move(0,-pulleyRadius - 0.5*wallthickness - pulleyClearance,0,relative=True);
 
cmds.select(enclosure[0]);
cmds.select(enclosure[0],bolt[0],r=True);
enclosure = cmds.polyBoolOp(op=2);
 
pulley1 = createPulley(-0.5*pulleyheight - washerThickness,0);
pulley2 = createPulley(0.5*pulleyheight + washerThickness,0);
  
rope1 = createRope(-0.5*pulleyheight - washerThickness,pulleyRadius);
rope2 = createRope(0.5*pulleyheight + washerThickness,pulleyRadius);
  
bolt1 = createBoltHole(boltMountDistance);
bolt2 = createBoltHole(-boltMountDistance);
  
enclosure = subtractObject(enclosure,bolt1);
enclosure = subtractObject(enclosure,bolt2);
enclosure = subtractObject(enclosure,rope1);
enclosure = subtractObject(enclosure,rope2);
  
 #cmds.rotate(0,0,'90deg');
 #cmds.move(0,enclosureWidth/2.0,0);
  
cmds.select(pulley1[0]);
cmds.move(-1.35*pulleyRadius,pulleyheight/2.0,1.015*pulleyRadius);
  
cmds.select(pulley2[0]);
cmds.move(-1.35*pulleyRadius,pulleyheight/2.0,-1.015*pulleyRadius);
  
cmds.select(enclosure[0],pulley1[0]);
cmds.align(y='Min',alignToLead=True);
cmds.align(x='Stack',alignToLead=True);
 
cmds.select(enclosure[0]);
cmds.move(1,0,0,r=True);
#cmds.rotate('90deg',0,0);
  
group = cmds.group(enclosure[0],pulley1[0],pulley2[0],n='scene');
cmds.rotate('90deg',0,0);
#  cmds.polySeparate(group);
cmds.ungroup(group);
